set more off

local j = 1
forvalues y = 2008(1)2015 {

	insheet using "uninsured/sahie`y'.csv", clear

	//keep if agecat == 0 // all ages (under 65)
	keep if agecat == 1 // ages 18-64
	keep if racecat == 0 // all races
	keep if sexcat == 0 // both sexes
	//keep if iprcat == 0 // all income groups
	keep if iprcat == 3 // <= 138% FPL

	destring pctui, replace force
	destring nipr, replace force
	
	capture rename stcou geofips
	local geoid statefips countyfips
	if year <= 2013 local geoid geofips
	keep year `geoid' pctui nipr
	
	if `j' > 1 append using tmp
	
	save tmp, replace
	
	local j = `j' + 1

}
erase tmp.dta

tostring statefips, replace
replace statefips = "0" + statefips if length(statefips) == 1
tostring countyfips, replace
replace countyfips = "0" + countyfips if length(countyfips) != 3
replace countyfips = "0" + countyfips if length(countyfips) != 3
gen geofips2 = statefips + countyfips
destring geofips2, replace force
replace geofips = geofips2 if mi(geofips)
drop geofips2 
drop countyfips statefips
drop if mi(pctui)
rename geofips fips

gen pcti = 100 - pctui

reshape i fips
reshape j year
reshape xij pctui pcti nipr
reshape wide

save "uninsured/uninsured", replace

